package com.woniuxy.shop.dao;

import java.util.ArrayList;
import java.util.List;

import com.woniuxy.shop.entity.Goods;
import com.woniuxy.shop.entity.Remark;

/**
*@author 作者：
*version 创建时间：
*类说明：
*/
public class RemarkDao {
	/**
	 * 增加评论信息
	 * @param r
	 */
	public void add(Remark r) {
		Object[] obs = {r.getUserid(),r.getGoodsid(),r.getScore(),r.getContent(),r.getRemarktime(),r.getStatus()};
		String sql = "insert into shop_remark(userid,goodsid,score,content,remarktime,status) values(?,?,?,?,?,?)";
		DbHelper.excuteSQL(sql, obs);
	}
	
	/**
	 * 删除评论信息
	 * @param id
	 */
	public void delete(int id) {
		String sql = "delete from shop_remark where id=?";
		DbHelper.excuteSQL(sql, id);
	}
	
	/**
	 * 修改评分
	 * @param id
	 * @param score
	 */
	public void updateScore(int id,int score) {
		String sql = "update shop_remark set score=? where id=?";
		DbHelper.excuteSQL(sql,score, id);
	}
	
	/**
	 * 修改评论内容
	 * @param id
	 * @param score
	 */
	public void updateContent(int id,int content) {
		String sql = "update shop_remark set content=? where id=?";
		DbHelper.excuteSQL(sql,content, id);
	}
	
	/**
	 * 修改评论评论状态
	 * @param id
	 * @param score
	 */
	public void updateStatus(int id,int status) {
		String sql = "update shop_remark set status=? where id=?";
		DbHelper.excuteSQL(sql,status, id);
	}
	
	/**
	 * 查看某评论的状态
	 */
	public String getStatus(int id) {
		String sql = "select status from shop_remark where id=?";
		return DbHelper.executeQueryForScalar(sql, id);
	}
	
	/**
	 * 查看某评论的所有信息
	 * @param id
	 * @return
	 */
	public Remark getRemark(int id) {
		String sql = "select * from shop_remark where id=?";
		return DbHelper.executeQueryForSingleRow(sql, Remark.class,id);
	}
	
	/**
	 * 获取所有的评论
	 * @param id
	 * @return
	 */
	public List<Remark> getRemarks(int id){
		String sql = "select * from shop_remark where id=?";
		List<Remark> remarks = new ArrayList<>();
		remarks = DbHelper.executeQuery(sql, Remark.class, id);
		return remarks;
	}

	/**
	 * 查询多条件的个数
	 * @param r
	 * @return
	 */
	public int getRemarkAsLimit(Remark r) {
		String sql = "select count(*) from shop_remark where 1=1";
		if(r.getUserid()!=0) {
			sql += " and userid="+r.getUserid();
		}
		if(r.getGoodsid()!=0) {
			sql += " and goodsid="+r.getGoodsid();
		}
		if(r.getMinscore()!=0) {
			sql += " and score>="+r.getMinscore();
		}
		if(r.getMaxscore()!=0) {
			sql += " and score<="+r.getMaxscore();
		}
		if(r.getStarttime()!=null&&!r.getStarttime().trim().equals("")) {
			sql += " and remarktime>='"+r.getStarttime()+"'";
		}
		if(r.getEndtime()!=null&&!r.getEndtime().trim().equals("")) {
			sql += " and remarktime<='"+r.getEndtime()+"'";
		}
		if(r.getContent()!=null&&!r.getContent().trim().equals("")) {
			sql += " and content like'%"+r.getContent()+"%'";
		}
		if(r.getStatus()!=null&&!r.getStatus().trim().equals("")) {
			sql += " and status='"+r.getStatus()+"'";
		}
		
		
		return ((Long)DbHelper.executeQueryForScalar(sql)).intValue();
	}
	
	/**
	 * 分页查询多条件信息
	 * @param page
	 * @param pagesize
	 * @param r
	 * @return
	 */
	public List<Remark> getRemarkByPage(int page, int pagesize, Remark r) {
		String sql = "select *from shop_remark where 1=1";
		if(r.getUserid()!=0) {
			sql += " and userid="+r.getUserid();
		}
		if(r.getGoodsid()!=0) {
			sql += " and goodsid="+r.getGoodsid();
		}
		if(r.getMinscore()!=0) {
			sql += " and score>="+r.getMinscore();
		}
		if(r.getMaxscore()!=0) {
			sql += " and score<="+r.getMaxscore();
		}
		if(r.getStarttime()!=null&&!r.getStarttime().trim().equals("")) {
			sql += " and remarktime>='"+r.getStarttime()+"'";
		}
		if(r.getEndtime()!=null&&!r.getEndtime().trim().equals("")) {
			sql += " and remarktime<='"+r.getEndtime()+"'";
		}
		if(r.getContent()!=null&&!r.getContent().trim().equals("")) {
			sql += " and content like'%"+r.getContent()+"%'";
		}
		if(r.getStatus()!=null&&!r.getStatus().trim().equals("")) {
			sql += " and status='"+r.getStatus()+"'";
		}
		
		sql += " limit ?,?";
		return DbHelper.executeQuery(sql, Remark.class, (page-1)*pagesize,pagesize);
	}
	
}
